﻿using System;
using System.Collections.Generic;
using UnityEngine;

    public class Message
    {
        private Client client;
        List<String> messageString=new List<String>();
        List<String> onlineListString=new List<String>();

        Dictionary<int, String> onlineList =new Dictionary<int, String>();
        Dictionary<int, String> userMap =new Dictionary<int, String>();
         Data data=new Data();

         private bool isUpdateList=false;
        public Client Client
        {
            set { client=value; }
        }
        public List<String> getMessage
        {
            get { return messageString; }
        }
        public List<String> getOnlineList
        {
            get { return onlineListString; }
        }

        public Dictionary<int, String> getonlineList
        {
            get { return onlineList; }
        }
        
        public bool IsUpdateList
        {
            get { return isUpdateList; }
            set {isUpdateList = value; }
        }
        public void SetData(Data Indata)
        {
            this.data=Indata;
            
            if (data.name!=null && data.id!=-1 && data.content==null && data.kind==1)
            {
                if (!userMap.ContainsKey(data.id))
                {
                    userMap.Add(data.id,data.name);
                }
                DataClear();
            }
            else if (data.name!=null && data.id!=-1 && data.content==null)
            {
                Debug.Log(data.name);
                if (!onlineList.ContainsKey(data.id))
                {
                    onlineList.Add(data.id,data.name);
                    onlineListString.Add(data.name);
                    messageString.Add("用户"+data.name + "上线" );
                    DataClear();
                }
            }
            else if (data.name==null && data.id!=-1 && data.content==null)
            {
                isUpdateList = true;
                onlineList.TryGetValue(data.id,out String name);
                messageString.Add("用户"+name + "下线" );
                if (onlineListString.Exists(t => t == name))
                {
                    onlineListString.Remove(name);
                }
                DataClear();

            }
            
            else if (data.name==null && data.id!=-1 && data.content!=null)
            {
                userMap.TryGetValue(data.id, out String userName);
                messageString.Add(userName + "在"+data.time +"发送："+ data.content);
                DataClear();
            }
        }

        public void StringToData(String content)
        {
            data.content = content;
            client.Send(data);
            DataClear();
        }
        
        public void SendName(String name)
        {
            data.name = name;
            client.Send(data);
            DataClear();
        }
        public void DataClear()
        {
            data.id = -1;
            data.name = null;
            data.content = null;
            data.time = null;
            data.kind = -1;
        }
        




        

    }